import random
import time
import pygame

# 常量
W, H = 288, 512
FPS = 60

# 设置
pygame.init()
SCREEN = pygame.display.set_mode((W, H))  # 游戏窗口
pygame.display.set_caption('满月的第一个pygame Demo')
CLOCK = pygame.time.Clock()  # 设置游戏时钟

# 加载游戏资源
bird = pygame.image.load('./assets/image/tobyfox2.png')
start_music = pygame.mixer.Sound('./assets/music/谪仙.flac')


class Bird:
    def __init__(self, x, y):
        self.originImage = bird
        self.image = pygame.transform.scale(bird, (100, 100))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.clickLeft = False

    def update(self, flap=False):
        if not self.clickLeft:
            self.rect.y += 3

    def die(self):
        return

    def move(self, pos=(0, 0)):
        if self.clickLeft:
            self.rect.x = pos[0] - self.image.get_width()/2
            self.rect.y = pos[1] - self.image.get_height()/2

    def fly(self):
        print('fly')


def main():
    while True:
        # start_music.play()
        menu_window()
        end_window(game_window())


def menu_window():
    global bird
    bird_x = (W - bird.get_width()) / 2
    bird_y = (H - bird.get_height()) / 2
    while True:
        for event in pygame.event.get():
            # print(event.type)
            if event.type == pygame.QUIT:
                quit()
            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                return
                # bird = pygame.transform.scale(bird, (100, 100))
                # return

        # bird_x -= 4
        # if bird_x <= -bird.get_width():
        #     bird_x = W

        # SCREEN.blit(bird, (bird_x, bird_y))
        pygame.draw.rect(SCREEN, '#53c9c1', (0, 0, W, H))
        pygame.display.update()
        CLOCK.tick(FPS)


def game_window():
    player = Bird(0, 100)
    while True:
        for event in pygame.event.get():
            # print(event)
            if event.type == pygame.QUIT:
                quit()
            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                print(1)
                # return
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == pygame.BUTTON_LEFT:
                player.clickLeft = True
                player.move(event.pos)
            if event.type == pygame.MOUSEMOTION:
                player.move(event.pos)
            if event.type == pygame.MOUSEBUTTONUP and event.button == pygame.BUTTON_LEFT:
                player.clickLeft = False

        if (player.rect.y + player.image.get_height()) >= H:
            print('die')
            return

        pygame.draw.rect(SCREEN, '#3178cd', (0, 0, W, H))
        SCREEN.blit(player.image, player.rect)
        player.update()
        pygame.display.update()
        CLOCK.tick(FPS)


def end_window(result):
    while True:
        for event in pygame.event.get():
            # print(event.type)
            if event.type == pygame.QUIT:
                quit()
            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                print('game over')
                return
                # return
            if event.type == pygame.MOUSEBUTTONUP and event.button == pygame.BUTTON_LEFT:
                # print(event.pos)
                print(event)

        pygame.draw.rect(SCREEN, '#e8534e', (0, 0, W, H))
        pygame.draw.rect(SCREEN, 'green', ((W-200)/2, (H-100)/2, 200, 100))
        pygame.display.update()
        CLOCK.tick(FPS)


main()
